package com.korosmatick.samplejms.jms;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import org.springframework.jms.support.JmsUtils;
import org.springframework.stereotype.Component;

import com.korosmatick.samplejms.domain.DummyMessage;

@Component
public class JMSMessageListener implements MessageListener {
	
	public void onMessage(Message message) {
		MapMessage mapMessage = (MapMessage) message;
		try {
			DummyMessage dummyMessage = new DummyMessage();
			dummyMessage.setId(mapMessage.getInt("id"));
			dummyMessage.setContent(mapMessage.getString("content"));
			displayDummyMessage(dummyMessage);
		} catch (JMSException e) {
			throw JmsUtils.convertJmsAccessException(e);
		}
	}

	private void displayDummyMessage(DummyMessage dummyMessage) {
		System.out.println("Received DummyMessage #" + dummyMessage.getId() + " : " + dummyMessage.getContent());
	}
}